
 <!DOCTYPE HTML>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  
    <title>自己写一个监控RabbitMQ的脚本 | Zong&#39;s blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="Zong">
    
    <meta name="description" content="一、前言RabbitMQ自己已经有一个挺好的管理监控界面，可以看到非常详细的消息队列信息，但是对于运维人员来说，还缺少了很重要的告警功能。自己写一个脚本也是很有必要的，在网上也有很多参考，主要思路是利用RabbitMQ自带管理模块的RESTful API取值，发送到zabbix，由zabbix负责告">
    
    
    
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
</head>
</html>
  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.svg" alt="Zong&#39;s blog" title="Zong&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Zong&#39;s blog">Zong&#39;s blog</a></h1>
				<h2 class="blog-motto">日常积累，技术分享</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">Home</a></li>
					
						<li><a href="/archives">Archives</a></li>
					
						<li><a href="/categories/运维">运维</a></li>
					
						<li><a href="/categories/容器架构">容器架构</a></li>
					
					<li>
					
					<form class="search" action="//baidu.com/s" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="text" id="search" name="wd" autocomplete="off" maxlength="20" placeholder="Search" />
                        <input name=tn type=hidden value="bds">
                        <input name=cl type=hidden value="3">
                        <input name=ct type=hidden value="2097152">
						<input type="hidden" name="si" value="www.lstop.pub">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/10/10/自己写一个监控RabbitMQ的脚本/" title="自己写一个监控RabbitMQ的脚本" itemprop="url">自己写一个监控RabbitMQ的脚本</a>
  </h1>
  <p class="article-author">By
    
      <a href="http://www.lstop.pub" title="Zong">Zong</a>
    </p>
  <p class="article-time">
    <time datetime="2016-10-10T03:36:37.000Z" itemprop="datePublished">2016-10-10</time>
    
  </p>
</header>

	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、前言"><span class="toc-number">1.</span> <span class="toc-text">一、前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、RabbitMQ-RESTful-API"><span class="toc-number">2.</span> <span class="toc-text">二、RabbitMQ RESTful API</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、zabbix-sender"><span class="toc-number">3.</span> <span class="toc-text">三、zabbix sender</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、python代码"><span class="toc-number">4.</span> <span class="toc-text">四、python代码</span></a></li></ol>
		</div>
		
		<h1 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h1><p>RabbitMQ自己已经有一个挺好的管理监控界面，可以看到非常详细的消息队列信息，但是对于运维人员来说，还缺少了很重要的告警功能。<br>自己写一个脚本也是很有必要的，在网上也有很多参考，主要思路是利用RabbitMQ自带管理模块的RESTful API取值，发送到zabbix，由zabbix负责告警阀值判断和发送告警</p>
<h1 id="二、RabbitMQ-RESTful-API"><a href="#二、RabbitMQ-RESTful-API" class="headerlink" title="二、RabbitMQ RESTful API"></a>二、RabbitMQ RESTful API</h1><p>首先要认识一下RabbitMQ的API，看看我们能取到什么东西，推荐看看官方的help<br>下面的例子我们在程序中会用到</p>
<p>查看MQ总体情况</p>
<blockquote>
<p>官方解析：/api/overview Various random bits of information that describe the whole system.<br>例子：<br>curl -u guest:guest <a href="http://192.168.55.241:15672/api/overview" target="_blank" rel="noopener">http://192.168.55.241:15672/api/overview</a></p>
</blockquote>
<p>返回的是json格式的响应</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;management_version&quot;: &quot;3.0.0&quot;,</span><br><span class="line">    &quot;statistics_level&quot;: &quot;fine&quot;,</span><br><span class="line">    &quot;exchange_types&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;name&quot;: &quot;topic&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;AMQP topic exchange, as per the AMQP specification&quot;,</span><br><span class="line">            &quot;enabled&quot;: true</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;name&quot;: &quot;fanout&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;AMQP fanout exchange, as per the AMQP specification&quot;,</span><br><span class="line">            &quot;enabled&quot;: true</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;name&quot;: &quot;direct&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;AMQP direct exchange, as per the AMQP specification&quot;,</span><br><span class="line">            &quot;enabled&quot;: true</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;name&quot;: &quot;headers&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;AMQP headers exchange, as per the AMQP specification&quot;,</span><br><span class="line">            &quot;enabled&quot;: true</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    &quot;rabbitmq_version&quot;: &quot;3.0.0&quot;,</span><br><span class="line">    &quot;erlang_version&quot;: &quot;R15B01&quot;,</span><br><span class="line">    &quot;message_stats&quot;: &#123;</span><br><span class="line">        &quot;publish&quot;: 1104833,</span><br><span class="line">        &quot;publish_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 12.262779782167032,</span><br><span class="line">            &quot;interval&quot;: 298146914019,</span><br><span class="line">            &quot;last_event&quot;: 1476068131606</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;ack&quot;: 1177571,</span><br><span class="line">        &quot;ack_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 11.230039450092189,</span><br><span class="line">            &quot;interval&quot;: 57927468985,</span><br><span class="line">            &quot;last_event&quot;: 1476068130882</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;deliver&quot;: 1177572,</span><br><span class="line">        &quot;deliver_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 11.027518607238914,</span><br><span class="line">            &quot;interval&quot;: 57927468985,</span><br><span class="line">            &quot;last_event&quot;: 1476068130882</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;deliver_get&quot;: 1207878,</span><br><span class="line">        &quot;deliver_get_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 11.027518607238914,</span><br><span class="line">            &quot;interval&quot;: 57927468985,</span><br><span class="line">            &quot;last_event&quot;: 1476068130882</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;deliver_no_ack&quot;: 30306,</span><br><span class="line">        &quot;deliver_no_ack_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 1067361970,</span><br><span class="line">            &quot;last_event&quot;: 1476068115093</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;redeliver&quot;: 8,</span><br><span class="line">        &quot;redeliver_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 115179973,</span><br><span class="line">            &quot;last_event&quot;: 1476068099504</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;queue_totals&quot;: &#123;</span><br><span class="line">        &quot;messages&quot;: 0,</span><br><span class="line">        &quot;messages_ready&quot;: 0,</span><br><span class="line">        &quot;messages_unacknowledged&quot;: 0,</span><br><span class="line">        &quot;messages_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 255911999,</span><br><span class="line">            &quot;last_event&quot;: 1476068131232</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;messages_ready_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 255911999,</span><br><span class="line">            &quot;last_event&quot;: 1476068131232</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;messages_unacknowledged_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 255911999,</span><br><span class="line">            &quot;last_event&quot;: 1476068131232</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;object_totals&quot;: &#123;</span><br><span class="line">        &quot;consumers&quot;: 70,</span><br><span class="line">        &quot;queues&quot;: 27,</span><br><span class="line">        &quot;exchanges&quot;: 13,</span><br><span class="line">        &quot;connections&quot;: 176,</span><br><span class="line">        &quot;channels&quot;: 236</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;node&quot;: &quot;rabbit@rabbit01&quot;,</span><br><span class="line">    &quot;statistics_db_node&quot;: &quot;rabbit@rabbit01&quot;,</span><br><span class="line">    &quot;listeners&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;node&quot;: &quot;rabbit@rabbit01&quot;,</span><br><span class="line">            &quot;protocol&quot;: &quot;amqp&quot;,</span><br><span class="line">            &quot;ip_address&quot;: &quot;::&quot;,</span><br><span class="line">            &quot;port&quot;: 5672</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    &quot;contexts&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;node&quot;: &quot;rabbit@rabbit01&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;RabbitMQ Management&quot;,</span><br><span class="line">            &quot;path&quot;: &quot;/&quot;,</span><br><span class="line">            &quot;port&quot;: 15672</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;node&quot;: &quot;rabbit@rabbit01&quot;,</span><br><span class="line">            &quot;description&quot;: &quot;Redirect to port 15672&quot;,</span><br><span class="line">            &quot;path&quot;: &quot;/&quot;,</span><br><span class="line">            &quot;port&quot;: 55672,</span><br><span class="line">            &quot;ignore_in_use&quot;: true</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>查看单个队列</p>
<blockquote>
<p>官方解析：/api/queues/vhost/name An individual queue. To PUT a queue, you will need a body looking something like this:<br>{“auto_delete”:false,”durable”:true,”arguments”:[],”node”:”rabbit@smacmullen”}<br>All keys are optional.</p>
<p>例子：%2F代表vhost为/<br>curl -u guest:guest <a href="http://192.168.55.241:15672/api/queues/%2F/wxq.msgweb.push.wgw.0" target="_blank" rel="noopener">http://192.168.55.241:15672/api/queues/%2F/wxq.msgweb.push.wgw.0</a></p>
</blockquote>
<p>返回的是json格式的响应</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;policy&quot;: &quot;&quot;,</span><br><span class="line">    &quot;exclusive_consumer_tag&quot;: &quot;&quot;,</span><br><span class="line">    &quot;messages_ready&quot;: 0,</span><br><span class="line">    &quot;messages_unacknowledged&quot;: 0,</span><br><span class="line">    &quot;messages&quot;: 0,</span><br><span class="line">    &quot;consumers&quot;: 1,</span><br><span class="line">    &quot;active_consumers&quot;: 1,</span><br><span class="line">    &quot;memory&quot;: 55680,</span><br><span class="line">    &quot;backing_queue_status&quot;: &#123;</span><br><span class="line">        &quot;q1&quot;: 0,</span><br><span class="line">        &quot;q2&quot;: 0,</span><br><span class="line">        &quot;delta&quot;: [</span><br><span class="line">            &quot;delta&quot;,</span><br><span class="line">            &quot;undefined&quot;,</span><br><span class="line">            0,</span><br><span class="line">            &quot;undefined&quot;</span><br><span class="line">        ],</span><br><span class="line">        &quot;q3&quot;: 0,</span><br><span class="line">        &quot;q4&quot;: 0,</span><br><span class="line">        &quot;len&quot;: 0,</span><br><span class="line">        &quot;pending_acks&quot;: 0,</span><br><span class="line">        &quot;target_ram_count&quot;: &quot;infinity&quot;,</span><br><span class="line">        &quot;ram_msg_count&quot;: 0,</span><br><span class="line">        &quot;ram_ack_count&quot;: 0,</span><br><span class="line">        &quot;next_seq_id&quot;: 1,</span><br><span class="line">        &quot;persistent_count&quot;: 0,</span><br><span class="line">        &quot;avg_ingress_rate&quot;: 0,</span><br><span class="line">        &quot;avg_egress_rate&quot;: 0,</span><br><span class="line">        &quot;avg_ack_ingress_rate&quot;: 0,</span><br><span class="line">        &quot;avg_ack_egress_rate&quot;: 0</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;messages_details&quot;: &#123;</span><br><span class="line">        &quot;rate&quot;: 0,</span><br><span class="line">        &quot;interval&quot;: 10001981,</span><br><span class="line">        &quot;last_event&quot;: 1476068574166</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;messages_ready_details&quot;: &#123;</span><br><span class="line">        &quot;rate&quot;: 0,</span><br><span class="line">        &quot;interval&quot;: 10001981,</span><br><span class="line">        &quot;last_event&quot;: 1476068574166</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;messages_unacknowledged_details&quot;: &#123;</span><br><span class="line">        &quot;rate&quot;: 0,</span><br><span class="line">        &quot;interval&quot;: 10001981,</span><br><span class="line">        &quot;last_event&quot;: 1476068574166</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;incoming&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;stats&quot;: &#123;</span><br><span class="line">                &quot;publish&quot;: 763,</span><br><span class="line">                &quot;publish_details&quot;: &#123;</span><br><span class="line">                    &quot;rate&quot;: 0,</span><br><span class="line">                    &quot;interval&quot;: 75305976,</span><br><span class="line">                    &quot;last_event&quot;: 1476068545922</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;exchange&quot;: &#123;</span><br><span class="line">                &quot;name&quot;: &quot;wxq_appmsg_exchange&quot;,</span><br><span class="line">                &quot;vhost&quot;: &quot;/&quot;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    &quot;deliveries&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;stats&quot;: &#123;</span><br><span class="line">                &quot;deliver_get&quot;: 774,</span><br><span class="line">                &quot;deliver_get_details&quot;: &#123;</span><br><span class="line">                    &quot;rate&quot;: 0,</span><br><span class="line">                    &quot;interval&quot;: 44411017,</span><br><span class="line">                    &quot;last_event&quot;: 1476068470753</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;deliver_no_ack&quot;: 774,</span><br><span class="line">                &quot;deliver_no_ack_details&quot;: &#123;</span><br><span class="line">                    &quot;rate&quot;: 0,</span><br><span class="line">                    &quot;interval&quot;: 44411017,</span><br><span class="line">                    &quot;last_event&quot;: 1476068470753</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;channel_details&quot;: &#123;</span><br><span class="line">                &quot;name&quot;: &quot;192.168.65.158:64326 -&gt; 192.168.55.241:5672 (1)&quot;,</span><br><span class="line">                &quot;number&quot;: 1,</span><br><span class="line">                &quot;connection_name&quot;: &quot;192.168.65.158:64326 -&gt; 192.168.55.241:5672&quot;,</span><br><span class="line">                &quot;peer_port&quot;: 64326,</span><br><span class="line">                &quot;peer_host&quot;: &quot;192.168.65.158&quot;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    &quot;message_stats&quot;: &#123;</span><br><span class="line">        &quot;deliver_get&quot;: 774,</span><br><span class="line">        &quot;deliver_get_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 44411017,</span><br><span class="line">            &quot;last_event&quot;: 1476068470753</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;deliver_no_ack&quot;: 774,</span><br><span class="line">        &quot;deliver_no_ack_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 44411017,</span><br><span class="line">            &quot;last_event&quot;: 1476068470753</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;publish&quot;: 763,</span><br><span class="line">        &quot;publish_details&quot;: &#123;</span><br><span class="line">            &quot;rate&quot;: 0,</span><br><span class="line">            &quot;interval&quot;: 75305976,</span><br><span class="line">            &quot;last_event&quot;: 1476068545922</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;consumer_details&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;channel_details&quot;: &#123;</span><br><span class="line">                &quot;name&quot;: &quot;192.168.65.158:64326 -&gt; 192.168.55.241:5672 (1)&quot;,</span><br><span class="line">                &quot;number&quot;: 1,</span><br><span class="line">                &quot;connection_name&quot;: &quot;192.168.65.158:64326 -&gt; 192.168.55.241:5672&quot;,</span><br><span class="line">                &quot;peer_port&quot;: 64326,</span><br><span class="line">                &quot;peer_host&quot;: &quot;192.168.65.158&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;queue_details&quot;: &#123;</span><br><span class="line">                &quot;name&quot;: &quot;wxq.msgweb.push.wgw.0&quot;,</span><br><span class="line">                &quot;vhost&quot;: &quot;/&quot;</span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;consumer_tag&quot;: &quot;amq.ctag-jKVWrQSs7dbX9VaFfOZiYA&quot;,</span><br><span class="line">            &quot;exclusive&quot;: false,</span><br><span class="line">            &quot;ack_required&quot;: false</span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    &quot;name&quot;: &quot;wxq.msgweb.push.wgw.0&quot;,</span><br><span class="line">    &quot;vhost&quot;: &quot;/&quot;,</span><br><span class="line">    &quot;durable&quot;: true,</span><br><span class="line">    &quot;auto_delete&quot;: false,</span><br><span class="line">    &quot;arguments&quot;: &#123;&#125;,</span><br><span class="line">    &quot;node&quot;: &quot;rabbit@rabbit01&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="三、zabbix-sender"><a href="#三、zabbix-sender" class="headerlink" title="三、zabbix sender"></a>三、zabbix sender</h1><p>区别于在zabbix agent添加item，sender可以主动发送item值到zabbix server<br>可以先规划好在zabbix server中使用的host和item名字</p>
<blockquote>
<p>例如<br>rabbitmq.[rabbit@wx-pro-server01,messages_ready] 待处理消息总数<br>rabbitmq.[rabbit@wx-pro-server01,messages_unacknowledged] 未收到ack响应消息总数<br>rabbitmq.queues[/,queue_messages_ready,wxq_msg_send] wxq_msg_send队列的待处理消息数<br>rabbitmq.queues[/,queue_active_consumers,wxq_msg_send] wxq_msg_send队列的活动连接数</p>
</blockquote>
<p>这样就可以通过python取值，保存在临时文件中，再send到zabbix server。</p>
<blockquote>
<p>zabbix_sender使用方法<br>/usr/bin/zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -i /tmp/tmp5ai4uI -s ser01-rabbitmq<br>-c agent配置文件<br>-i 临时文件，用于保存多个item和对应值，每行一个item<br>-s 在zabbix server上配置的host名字</p>
</blockquote>
<h1 id="四、python代码"><a href="#四、python代码" class="headerlink" title="四、python代码"></a>四、python代码</h1><p>做好上面的准备工作，就可以开始写脚本了，不多说了直接放代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/python</span></span><br><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">RabbitMq监控脚本 v1.0</span></span><br><span class="line"><span class="string">v1.1 监控channel的unacknowledged</span></span><br><span class="line"><span class="string">v1.0 监控overview、queue</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> urllib2</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> tempfile</span><br><span class="line"><span class="keyword">import</span> logging</span><br><span class="line"><span class="keyword">import</span> subprocess</span><br><span class="line"><span class="keyword">import</span> optparse</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line">logging.basicConfig(filename=<span class="string">'/var/log/zabbix/rabbitmq_zabbix.log'</span>, level=logging.WARNNING, format=<span class="string">'%(asctime)s %(levelname)s: %(message)s'</span>)</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RabbitMqApi</span><span class="params">(object)</span>:</span></span><br><span class="line"> <span class="string">''' 调用RabbitMQ的API类 '''</span></span><br><span class="line"> <span class="comment">######## json.loads()  transfer json data to python data</span></span><br><span class="line"> <span class="comment">######## json.dump()   transfer python data to json data</span></span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, user_name=<span class="string">'guest'</span>, password=<span class="string">'guest'</span>, host_name=<span class="string">'192.168.55.241'</span>, </span></span></span><br><span class="line"><span class="function"><span class="params">              protocol=<span class="string">'http'</span>, port=<span class="number">15672</span>, senderhostname=<span class="string">'ser01-rabbitmq'</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">                          zbconf=<span class="string">'/etc/zabbix/zabbix_agentd.conf'</span>)</span>:</span></span><br><span class="line">  self.user_name = user_name</span><br><span class="line">  self.password = password</span><br><span class="line">  self.host_name = host_name <span class="keyword">or</span> socket.gethostname()</span><br><span class="line">  self.protocol = protocol</span><br><span class="line">  self.port = port</span><br><span class="line">  self.senderhostname = senderhostname <span class="keyword">if</span> senderhostname <span class="keyword">else</span> host_name</span><br><span class="line">  self.conf = zbconf</span><br><span class="line"> <span class="comment"># 调用RabbitMQ的API类取原始数据，json格式</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">call_api</span><span class="params">(self, path)</span>:</span></span><br><span class="line">  <span class="string">'''</span></span><br><span class="line"><span class="string">     All URIs will server only resource of type application/json,and will require HTTP basic authentication. The default username and password is guest/guest.  /%sf is encoded for the default virtual host '/' </span></span><br><span class="line"><span class="string">  '''</span></span><br><span class="line">  url = <span class="string">'&#123;0&#125;://&#123;1&#125;:&#123;2&#125;/api/&#123;3&#125;'</span>.format(self.protocol, self.host_name, self.port, path)</span><br><span class="line">  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()</span><br><span class="line">  password_mgr.add_password(<span class="literal">None</span>, url, self.user_name, self.password)</span><br><span class="line">  handler = urllib2.HTTPBasicAuthHandler(password_mgr)</span><br><span class="line">  logging.debug(<span class="string">'Issue a rabbit API call to get data on '</span> + url)</span><br><span class="line">  <span class="keyword">return</span> json.loads(urllib2.build_opener(handler).open(url).read())</span><br><span class="line"> </span><br><span class="line"> <span class="comment"># 调用RabbitMQ的API类取原始数据，json格式</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">call_api_queue</span><span class="params">(self, queue_name)</span>:</span></span><br><span class="line">  <span class="string">'''</span></span><br><span class="line"><span class="string">     get queue detail</span></span><br><span class="line"><span class="string">  '''</span></span><br><span class="line">  url = <span class="string">'&#123;0&#125;://&#123;1&#125;:&#123;2&#125;/api/queues/%2F/&#123;3&#125;'</span>.format(self.protocol, self.host_name, self.port, queue_name)</span><br><span class="line">  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()</span><br><span class="line">  password_mgr.add_password(<span class="literal">None</span>, url, self.user_name, self.password)</span><br><span class="line">  handler = urllib2.HTTPBasicAuthHandler(password_mgr)</span><br><span class="line">  logging.debug(<span class="string">'Issue a rabbit API call to get data on '</span> + url)</span><br><span class="line">  <span class="keyword">return</span> json.loads(urllib2.build_opener(handler).open(url).read())</span><br><span class="line">  </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">call_api_channel</span><span class="params">(self, channel_name)</span>:</span></span><br><span class="line">  <span class="string">'''</span></span><br><span class="line"><span class="string">     get channel detail</span></span><br><span class="line"><span class="string">  '''</span></span><br><span class="line">  url = <span class="string">'&#123;0&#125;://&#123;1&#125;:&#123;2&#125;/api/channels/&#123;3&#125;'</span>.format(self.protocol, self.host_name, self.port, urllib2.quote(channel_name))</span><br><span class="line">  password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()</span><br><span class="line">  password_mgr.add_password(<span class="literal">None</span>, url, self.user_name, self.password)</span><br><span class="line">  handler = urllib2.HTTPBasicAuthHandler(password_mgr)</span><br><span class="line">  logging.debug(<span class="string">'Issue a rabbit API call to get data on '</span> + url)</span><br><span class="line">  <span class="keyword">return</span> json.loads(urllib2.build_opener(handler).open(url).read())</span><br><span class="line">  </span><br><span class="line"> <span class="comment"># 把数据与item对应写进临时文件</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">_get_queue_data</span><span class="params">(self, queue, tmpfile)</span>:</span></span><br><span class="line">  <span class="keyword">for</span> item <span class="keyword">in</span> [ <span class="string">'memory'</span>,<span class="string">'messages'</span>,<span class="string">'messages_ready'</span>,<span class="string">'messages_unacknowledged'</span>,<span class="string">'consumers'</span>,<span class="string">'active_consumers'</span> ]:</span><br><span class="line">   <span class="comment">#key = rabbitmq.queues[/,queue_memory,queue.helloWorld]</span></span><br><span class="line">   key = <span class="string">'"rabbitmq.queues[&#123;0&#125;,queue_&#123;1&#125;,&#123;2&#125;]"'</span>.format(queue[<span class="string">'vhost'</span>], item, queue[<span class="string">'name'</span>])</span><br><span class="line">   <span class="comment">### if item is in queue,value=queue[item],else value=0</span></span><br><span class="line">   value = queue.get(item, <span class="number">0</span>)</span><br><span class="line">   logging.debug(<span class="string">"SENDER_DATA: - %s %s"</span> % (key,value))</span><br><span class="line">   tmpfile.write(<span class="string">"- %s %s\n"</span> % (key, value))</span><br><span class="line">  <span class="comment">##  This is a non standard bit of information added after the standard items</span></span><br><span class="line">  <span class="keyword">for</span> item <span class="keyword">in</span> [<span class="string">'deliver_get'</span>, <span class="string">'publish'</span>]:</span><br><span class="line">   key = <span class="string">'"rabbitmq.queues[&#123;0&#125;,queue_message_stats_&#123;1&#125;,&#123;2&#125;]"'</span>.format(queue[<span class="string">'vhost'</span>], item, queue[<span class="string">'name'</span>])</span><br><span class="line">   value = queue.get(<span class="string">'message_stats'</span>, &#123;&#125;).get(item, <span class="number">0</span>)</span><br><span class="line">   logging.debug(<span class="string">"SENDER_DATA: - %s %s"</span> % (key,value))</span><br><span class="line">   tmpfile.write(<span class="string">"- %s %s\n"</span> % (key, value))</span><br><span class="line"> <span class="comment"># 使用zabbix_sender把item发送到zabbix server</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">_send_queue_data</span><span class="params">(self, tmpfile)</span>:</span></span><br><span class="line">  <span class="string">'''Send the queue data to Zabbix.'''</span></span><br><span class="line">  <span class="string">'''Get key value from temp file. '''</span></span><br><span class="line">  args = <span class="string">'/usr/bin/zabbix_sender -c &#123;0&#125; -i &#123;1&#125;'</span></span><br><span class="line">  <span class="keyword">if</span> self.senderhostname:</span><br><span class="line">   args = args + <span class="string">" -s "</span> + self.senderhostname</span><br><span class="line">  return_code = <span class="number">0</span></span><br><span class="line">  process = subprocess.Popen(args.format(self.conf, tmpfile.name),</span><br><span class="line">                                     shell=<span class="literal">True</span>, stdout=subprocess.PIPE,</span><br><span class="line">                                     stderr=subprocess.PIPE)</span><br><span class="line">  out, err = process.communicate()</span><br><span class="line">  logging.debug(<span class="string">"Finished sending data"</span>)</span><br><span class="line">  return_code = process.wait()</span><br><span class="line">  logging.info(<span class="string">"Found return code of "</span> + str(return_code))</span><br><span class="line">  <span class="keyword">if</span> return_code != <span class="number">0</span>:</span><br><span class="line">   logging.warning(out)</span><br><span class="line">   logging.warning(err)</span><br><span class="line">  <span class="keyword">else</span>:</span><br><span class="line">   logging.debug(err)</span><br><span class="line">   logging.debug(out)</span><br><span class="line">  <span class="keyword">return</span> return_code</span><br><span class="line">          	  </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">check_queue</span><span class="params">(self, queueName)</span>:</span></span><br><span class="line">  rdatafile = tempfile.NamedTemporaryFile(delete=<span class="literal">False</span>)</span><br><span class="line">  return_code = <span class="number">0</span></span><br><span class="line">  queue = self.call_api_queue(queueName)</span><br><span class="line">  self._get_queue_data(queue, rdatafile)</span><br><span class="line">  self._get_channel_data(queue, rdatafile)</span><br><span class="line">  rdatafile.close()</span><br><span class="line">  return_code = self._send_queue_data(rdatafile)</span><br><span class="line">  os.unlink(rdatafile.name)</span><br><span class="line">  <span class="keyword">return</span> return_code</span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">check_overview</span><span class="params">(self)</span>:</span></span><br><span class="line">  return_code = <span class="number">0</span></span><br><span class="line">  rdatafile = tempfile.NamedTemporaryFile(delete=<span class="literal">False</span>)</span><br><span class="line">  queue = self.call_api(<span class="string">'overview'</span>)</span><br><span class="line">  self._get_overview_data(queue, rdatafile)</span><br><span class="line">  rdatafile.close()</span><br><span class="line">  return_code = self._send_queue_data(rdatafile)</span><br><span class="line">  os.unlink(rdatafile.name)</span><br><span class="line">  <span class="keyword">return</span> return_code</span><br><span class="line">  </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">_get_overview_data</span><span class="params">(self, queue, tmpfile)</span>:</span></span><br><span class="line">  <span class="keyword">for</span> item <span class="keyword">in</span> [<span class="string">'publish_details'</span>, <span class="string">'ack_details'</span>, <span class="string">'deliver_get_details'</span>]:</span><br><span class="line">   key = <span class="string">"rabbitmq.[&#123;0&#125;,&#123;1&#125;_rate]"</span>.format(queue[<span class="string">'node'</span>], item)</span><br><span class="line">   value = queue.get(<span class="string">'message_stats'</span>).get(item).get(<span class="string">'rate'</span>, <span class="number">0</span>)</span><br><span class="line">   logging.debug(<span class="string">"SENDER_DATA: - &#123;0&#125; &#123;1:.2f&#125;\n"</span>.format(key, value))</span><br><span class="line">   tmpfile.write(<span class="string">"- &#123;0&#125; &#123;1:.2f&#125;\n"</span>.format(key, value))</span><br><span class="line">  <span class="keyword">for</span> item <span class="keyword">in</span> [<span class="string">'messages'</span>,<span class="string">'messages_ready'</span>,<span class="string">'messages_unacknowledged'</span>]:</span><br><span class="line">   key = <span class="string">"rabbitmq.[&#123;0&#125;,&#123;1&#125;]"</span>.format(queue[<span class="string">'node'</span>], item)</span><br><span class="line">   value = queue.get(<span class="string">'queue_totals'</span>).get(item,<span class="number">0</span>)</span><br><span class="line">   logging.debug(<span class="string">"SENDER_DATA: - %s %s"</span> % (key,value))</span><br><span class="line">   tmpfile.write(<span class="string">"- %s %s\n"</span> % (key, value))</span><br><span class="line">   </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">_get_channel_data</span><span class="params">(self, queue, tmpfile)</span>:</span> </span><br><span class="line">  <span class="string">'''</span></span><br><span class="line"><span class="string">  从queue获取queue name和channel name，组装zabbix的k-v并写进临时文件</span></span><br><span class="line"><span class="string">  '''</span></span><br><span class="line">  <span class="keyword">for</span> ch_detail <span class="keyword">in</span> queue[<span class="string">'consumer_details'</span>]:</span><br><span class="line">   key = <span class="string">"rabbitmq.[&#123;&#125;,&#123;&#125;]"</span>.format(queue[<span class="string">'name'</span>], ch_detail.get(<span class="string">'channel_details'</span>).get(<span class="string">'name'</span>,<span class="string">'no_name'</span>))</span><br><span class="line">   value = self._get_channel_ack(ch_detail.get(<span class="string">'channel_details'</span>).get(<span class="string">'name'</span>,<span class="string">'no_name'</span>))</span><br><span class="line">   logging.debug(<span class="string">"SENDER_DATA: - %s %s"</span> % (key,value))</span><br><span class="line">   tmpfile.write(<span class="string">"- %s %s\n"</span> % (key, value))</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">_get_channel_ack</span><span class="params">(self, channel_name)</span>:</span></span><br><span class="line">  <span class="string">'''</span></span><br><span class="line"><span class="string">  使用api获取messages_unacknowledged值</span></span><br><span class="line"><span class="string">  '''</span></span><br><span class="line">  d = self.call_api_channel(channel_name)</span><br><span class="line">  <span class="keyword">return</span> d[<span class="string">'messages_unacknowledged'</span>]</span><br><span class="line">  </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="string">'''Command-line parameters and decoding for Zabbix use/consumption.'''</span></span><br><span class="line"> <span class="comment">#testapi = RabbitMqApi()</span></span><br><span class="line"> <span class="comment">#testapi.check_queue('wxq_msg_send')</span></span><br><span class="line"> parser = optparse.OptionParser()</span><br><span class="line"> parser.add_option(<span class="string">'-u'</span>, <span class="string">'--username'</span>, help=<span class="string">'RabbitMQ API username,default=guest'</span>,default=<span class="string">'guest'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'-p'</span>, <span class="string">'--password'</span>, help=<span class="string">'RabbitMQ API password,default=guest'</span>,default=<span class="string">'guest'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'--mqhost'</span>, help=<span class="string">'RabbitMQ API hostname,default=hostname'</span>,default=socket.gethostname())</span><br><span class="line"> parser.add_option(<span class="string">'--protocol'</span>, help=<span class="string">'RabbitMQ API protocol,default=http'</span>,default=<span class="string">'http'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'--port'</span>, help=<span class="string">'RabbitMQ API port,default=15672'</span>,default=<span class="number">15672</span>)</span><br><span class="line"> parser.add_option(<span class="string">'--host'</span>, help=<span class="string">'Zabbix Host name which the item belongs to,no default'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'--conf'</span>, help=<span class="string">'Zabbix agent configure file,default=/etc/zabbix/zabbix_agentd.conf'</span>,default=<span class="string">'/etc/zabbix/zabbix_agentd.conf'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'--queue'</span>, help=<span class="string">'RabbitMQ queue which you want to check,no default'</span>)</span><br><span class="line"> parser.add_option(<span class="string">'-o'</span>,<span class="string">'--overview'</span>, help=<span class="string">'check RabbitMQ overview'</span>,action=<span class="string">"store_true"</span>)</span><br><span class="line"> parser.add_option(<span class="string">'-c'</span>,<span class="string">'--count'</span>, help=<span class="string">'How many times to execute command'</span>, type=<span class="string">'int'</span>, default=<span class="number">1</span>)</span><br><span class="line"> parser.add_option(<span class="string">'-i'</span>,<span class="string">'--interval'</span>, help=<span class="string">'Command execute interval'</span>, type=<span class="string">'int'</span>, default=<span class="number">0</span>)</span><br><span class="line"> (options, args) = parser.parse_args()</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> options.host:</span><br><span class="line">  parser.error(<span class="string">"请输入zabbix里定义的hostname"</span>)</span><br><span class="line"> <span class="keyword">elif</span> (<span class="keyword">not</span> options.queue <span class="keyword">and</span> <span class="keyword">not</span> options.overview):</span><br><span class="line">  parser.error(<span class="string">"请指定-o或者--queue，选择检查mq总体指标或者某个队列指标"</span>)</span><br><span class="line"> logging.debug(<span class="string">"Started trying to process data"</span>)</span><br><span class="line"> api = RabbitMqApi(user_name=options.username, password=options.password, host_name=options.mqhost, </span><br><span class="line">                  protocol=options.protocol, port=options.port, senderhostname=options.host,</span><br><span class="line">                  zbconf=options.conf)</span><br><span class="line"> cnt = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> cnt &lt; options.count:                                               </span><br><span class="line">  <span class="keyword">if</span> options.overview:</span><br><span class="line">   api.check_overview()</span><br><span class="line">   time.sleep(options.interval)</span><br><span class="line">  <span class="keyword">elif</span> options.queue:</span><br><span class="line">   api.check_queue(options.queue)</span><br><span class="line">   time.sleep(options.interval)</span><br><span class="line">  cnt += <span class="number">1</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    main()</span><br></pre></td></tr></table></figure>

<p>脚本使用方式如下：</p>
<ul>
<li>监控mq总体情况： mqMon.py -o –mqhost 192.168.55.241 –host ser01-rabbitmq -c 5 -i 12</li>
<li>监控队列情况： mqMon.py –queue wxq_msg_send –mqhost 192.168.55.241 –host ser01-rabbitmq -c 5 -i 12</li>
</ul>
<blockquote>
<p>–mqhost RabbitMQ服务器ip<br>–host zabbix server上配置的host名字<br>–queue 队列名字<br>-o 获取overview数据<br>-c 执行5次<br>-i 间隔12秒<br>–help 查看帮助，有其他参数可选</p>
</blockquote>
<p>代码参考了 <a href="http://john88wang.blog.51cto.com/2165294/1745824" target="_blank" rel="noopener">http://john88wang.blog.51cto.com/2165294/1745824</a></p>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/监控/">监控</a><a href="/tags/RabbitMQ/">RabbitMQ</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/运维/">运维</a>
</div>



<div class="article-share" id="share">

  <div data-url="http://www.lstop.pub/2016/10/10/自己写一个监控RabbitMQ的脚本/" data-title="自己写一个监控RabbitMQ的脚本 | Zong&#39;s blog" data-tsina="" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/02/24/安装GitLab的一些记录/" title="安装GitLab的一些记录">
  <strong>PREVIOUS:</strong><br/>
  <span>
  安装GitLab的一些记录</span>
</a>
</div>


<div class="next">
<a href="/2016/09/20/使用bootcdn加速hexo中的js静态资源/"  title="使用bootcdn加速hexo中的js静态资源">
 <strong>NEXT:</strong><br/> 
 <span>使用bootcdn加速hexo中的js静态资源
</span>
</a>
</div>

</nav>

	
</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
  <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、前言"><span class="toc-number">1.</span> <span class="toc-text">一、前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、RabbitMQ-RESTful-API"><span class="toc-number">2.</span> <span class="toc-text">二、RabbitMQ RESTful API</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、zabbix-sender"><span class="toc-number">3.</span> <span class="toc-text">三、zabbix sender</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、python代码"><span class="toc-number">4.</span> <span class="toc-text">四、python代码</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Airtest/" title="Airtest">Airtest<sup>1</sup></a></li>
		
			<li><a href="/tags/DNS/" title="DNS">DNS<sup>1</sup></a></li>
		
			<li><a href="/tags/GitLab/" title="GitLab">GitLab<sup>1</sup></a></li>
		
			<li><a href="/tags/K8s/" title="K8s">K8s<sup>8</sup></a></li>
		
			<li><a href="/tags/Linux/" title="Linux">Linux<sup>1</sup></a></li>
		
			<li><a href="/tags/MongoDB/" title="MongoDB">MongoDB<sup>2</sup></a></li>
		
			<li><a href="/tags/OpenWrt/" title="OpenWrt">OpenWrt<sup>1</sup></a></li>
		
			<li><a href="/tags/Python/" title="Python">Python<sup>2</sup></a></li>
		
			<li><a href="/tags/RabbitMQ/" title="RabbitMQ">RabbitMQ<sup>1</sup></a></li>
		
			<li><a href="/tags/calico/" title="calico">calico<sup>1</sup></a></li>
		
			<li><a href="/tags/cdn/" title="cdn">cdn<sup>1</sup></a></li>
		
			<li><a href="/tags/docker/" title="docker">docker<sup>3</sup></a></li>
		
			<li><a href="/tags/docker-registry/" title="docker registry">docker registry<sup>1</sup></a></li>
		
			<li><a href="/tags/elasticsearch/" title="elasticsearch">elasticsearch<sup>3</sup></a></li>
		
			<li><a href="/tags/elk/" title="elk">elk<sup>3</sup></a></li>
		
			<li><a href="/tags/k8s/" title="k8s">k8s<sup>3</sup></a></li>
		
			<li><a href="/tags/kubernetes/" title="kubernetes">kubernetes<sup>1</sup></a></li>
		
			<li><a href="/tags/nginx/" title="nginx">nginx<sup>1</sup></a></li>
		
			<li><a href="/tags/python/" title="python">python<sup>1</sup></a></li>
		
			<li><a href="/tags/tomcat/" title="tomcat">tomcat<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
      <li><a href="http://www.v2ex.com/?r=zong400" target="_blank" title="V2EX">V2EX</a></li>
      <li><a href="http://hexo.io" target="_blank" title="Hexo">Hexo</a></li>
	  <li><a href="https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=s0bh6uzq" target="_blank" title="阿里云">阿里云</a></li>
	  <li><a href="https://cloud.tencent.com/redirect.php?redirect=1014&cps_key=5bd9deb84d4d9f34b65fb934e12d03e3&from=console" target="_blank" title="腾讯云">腾讯云</a></li>
    </ul>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	
	<div class="social-font" class="clearfix">
		
		
		
		
	</div>
		<p class="copyright">Hosted by <a href="https://pages.coding.me/" target="_blank" title="Coding Pages">Coding Pages</a></p>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wizicer/iceman" target="_blank" title="Iceman">Iceman</a> © 2020 
		
		<a href="http://www.lstop.pub" target="_blank" title="Zong">Zong</a>
		
		</p>
</div>
</footer>
    <script src="//cdn.staticfile.org/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>









  </body>
</html>

